package com.training2.bookstore.product.service;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.training2.bookstore.cache.RedisCache;
import com.training2.bookstore.product.model.Product;
@Service
@Primary
public class CachedProductService implements ProductService {
	@Autowired ProductServiceSdata raelService;
	@Autowired RedisCache Cache;
	
	static final String LIST_PRODUCT = "LIST_PRODUCT";

	public Product create1(Product p) {
		return raelService.create1(p);
	}

	public Product create(Product p) {
		return raelService.create(p);
	}


	public Collection<Product> listProduct() {
		
		Collection<Product> sp = new ArrayList<Product>();
		Class<? extends Collection> type = sp.getClass();
		Collection<Product> rst = Cache.readCache(LIST_PRODUCT, type);
		if(rst == null) {
			rst = raelService.listProduct();
			Cache.cacheIt(LIST_PRODUCT, rst);
					
		}
		return rst;
	}

	public Product update(Product p) {
		return raelService.update(p);
	}

	public Product remove(String id) {
		return raelService.remove(id);
	}

	public void storeProductImage(String productId, String storedFileId) {
		raelService.storeProductImage(productId, storedFileId);
	}

	public Collection<Product> search(String key) {
		return raelService.search(key);
	}



}
